﻿@{
    ViewBag.Title = "数据库链接字符串";
}
<h2>数据库链接字符串</h2>

@using (Html.BeginForm("ConnStr", "SQL", FormMethod.Post, new { id = "sqlform", @class = "form-horizontal" }))
{
    <div class="form-group">
        <label for="txtSourceIP" class="col-sm-2 control-label">服务器地址</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="txtSourceIP" placeholder="服务器地址">
        </div>
    </div>
    <div class="form-group">
        <label for="txtCatalog" class="col-sm-2 control-label">数据库名称</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="txtCatalog" placeholder="数据库名称">
        </div>
    </div>
    <div class="form-group">
        <label for="checkMode" class="col-sm-2 control-label">验证模式</label>
        <div class="col-sm-10">
            <label class="radio-inline">
                <input type="radio" name="checkMode" id="checkModeWin" value="win"> Windows
            </label>
            <label class="radio-inline">
                <input type="radio" name="checkMode" id="checkModeSQL" value="sql" checked> SQL Server
            </label>
        </div>
    </div>
    <div class="form-group">
        <label for="txtUid" class="col-sm-2 control-label">用户名</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="txtUid" placeholder="用户名" />
        </div>
    </div>
    <div class="form-group">
        <label for="txtPwd" class="col-sm-2 control-label">密码</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="txtPwd" placeholder="密码" />
        </div>
    </div>
}
<hr />
@using (Html.BeginForm("", "", FormMethod.Post, new { id = "", @class = "form-horizontal" }))
{
    <div class="form-group">
        <div class="col-sm-3  col-sm-push-9">
            <button type="button" onclick="SqlConnStr()" class="btn btn-default">拼接</button>
            <button type="button" onclick="SqlConnStrAndEn()" class="btn btn-default">拼接并加密</button>
        </div>
        <label class="col-sm-4 col-sm-pull-3 text-left" style="padding-top:20px;margin-bottom:-10px;">
            原数据库连接字符串：
        </label>
    </div>
    <div class="form-group">
        <div class="col-xs-12">
            <input type="text" class="form-control col-xs-12" id="txtSqlConn" placeholder="原数据库连接字符串">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-3  col-sm-push-9">
            <button type="button" onclick="SqlConnStrEn()" class="btn btn-default">加密</button>
            <button type="button" onclick="SqlConnStrDe()" class="btn btn-default">解密</button>
        </div>
        <label class="col-sm-4 col-sm-pull-3 text-left" style="padding-top:20px;margin-bottom:-10px;">
            加密后数据库连接字符串：
        </label>
    </div>
    <div class="form-group">
        <div class="col-xs-12">
            <input type="text" class="form-control" id="txtSqlConnEn" placeholder="加密后数据库连接字符串">
        </div>
    </div>
}


<script>
    $(function () {
        $(":radio[name='checkMode']").change(function () {
            $("#txtUid").attr("disabled", false);
            $("#txtPwd").attr("disabled", false);
            if ($("input:radio[name='checkMode']:checked").val() == "win") {
                $("#txtUid").attr("disabled", true);
                $("#txtPwd").attr("disabled", true);
            }
        });
    });
    function SqlConnStr() {
        $("#txtSqlConn").val(GetSqlConnStr());
    }
    function SqlConnStrAndEn() {
        var sql = GetSqlConnStr();
        $("#txtSqlConn").val(sql);
        $.post("/Common/StrEncrypt", { str: sql }, function (datas) {
            if (datas.Status) {
                $("#txtSqlConnEn").val(datas.Data);
            }
            else {
                $("#txtSqlConnEn").val("");
            }
        })
    }
    function SqlConnStrEn() {
        var sql = $("#txtSqlConn").val();
        $.post("/Common/StrEncrypt", { str: sql }, function (datas) {
            if (datas.Status) {
                $("#txtSqlConnEn").val(datas.Data);
            }
            else {
                $("#txtSqlConnEn").val("");
            }
        })
    }
    function SqlConnStrDe() {
        var sql = $("#txtSqlConnEn").val();
        $.post("/Common/StrDecrypt", { str: sql }, function (datas) {
            if (datas.Status) {
                $("#txtSqlConn").val(datas.Data);
            }
            else {
                $("#txtSqlConn").val("");
            }
        })
    }

    function GetSqlConnStr() {
        var txtSourceIP = $("#txtSourceIP").val();
        var txtCatalog = $("#txtCatalog").val();

        if (!txtSourceIP || txtSourceIP == "")
            txtSourceIP = ".";
        if (!txtCatalog || txtCatalog == "")
            txtCatalog = "master";

        if ($("input:radio[name='checkMode']:checked").val() == "win") {
            return "Data Source=" + txtSourceIP + ";Initial Catalog=" + txtCatalog + ";Integrated Security=SSPI;";
        }
        else {

            return "Data Source=" + txtSourceIP + ";Initial Catalog=" + txtCatalog + ";uid=" + $("#txtUid").val() + ";pwd=" + $("#txtPwd").val() + ";";
        }
    }
</script>
